package com.tuanzi.loan.business.service.common.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.common.CommProductId;
import com.tuanzi.loan.business.entity.system.Dictionary;
import com.tuanzi.loan.business.repository.common.CommProductIdRepository;
import com.tuanzi.loan.business.repository.system.DictionaryRepository;
import com.tuanzi.loan.business.service.common.CommProductIdService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.service.CommonServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.channel.CommProductIdRequest;
import com.tuanzi.loan.vo.response.channel.CommProductIdResponse;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.model.CheckboxTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Ray 2017/10/31
 */
@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class CommProductIdServiceImpl extends CommonServiceImpl<CommProductId,Long> implements CommProductIdService {

	@Autowired
	private CommProductIdRepository commProductIdRepository;
	@Autowired
	private DictionaryRepository dictionaryRepository;

	@Override
	protected JpaRepository<CommProductId, Long> getRepository() {
		return commProductIdRepository;
	}

	@Override
	public List<CommProductId> findAll() {
		return commProductIdRepository.findAll();
	}

	@Override
	public List<CommProductId> findAllAppProducts() {
		return commProductIdRepository.findAllAppProducts();
	}

	@Override
	public List<CommProductId> findAllByParentId(String parentId) {
		CommProductId commProduct = new CommProductId();
		commProduct.setParentId(parentId);
		commProduct.setIsApp(1);
		Example<CommProductId> example = Example.of(commProduct);
		return commProductIdRepository.findAll(example);
	}

	@Override
	public Page<CommProductIdResponse> list(PageSearcher<CommProductIdRequest> pageSearcher) {
		return commProductIdRepository.list(pageSearcher.getSearchCondition(),pageSearcher.getPageable());
	}

	@Override
	public CommProductId findByProductId(Integer productId) {
		return commProductIdRepository.findByProductId(productId);
	}

	@Override
	public TreeNode createProductTreeNode(String selectNode) {
		List<CommProductId> all = this.findAll();
		//剔除没有分组的产品并进行分组
		Map<String, List<CommProductId>> groupProducts = all.stream()
				.filter(x -> StringUtils.isNotBlank(x.getParentId())).collect(Collectors.groupingBy(CommProductId::getParentId));

		//查询分组信息
		List<Dictionary> channelCode = dictionaryRepository.findAllByCodes(Lists.newArrayList(DictionaryCode.LARGE_COMMON_PRODUCT));
		TreeNode root = new CheckboxTreeNode("Root", null);

		//生成
		for (Dictionary dictionary : channelCode) {
			//一级名称
			CommProductId tempProduct = new CommProductId();
			tempProduct.setName(dictionary.getName());
			TreeNode secondNode = new CheckboxTreeNode(tempProduct, root);

			//为每个二级添加全部选项
			CommProductId allTempProduct = new CommProductId();
			allTempProduct.setName("全部");
			allTempProduct.setProductId(-1000);
			allTempProduct.setParentId(dictionary.getValue1());
			new CheckboxTreeNode(allTempProduct, secondNode);

			List<CommProductId> commProductIds = groupProducts.get(dictionary.getValue1());
			if(null != commProductIds && commProductIds.size() > 0){
				for (CommProductId product : commProductIds) {
					CheckboxTreeNode thirdNode = new CheckboxTreeNode(product, secondNode);
					if(org.apache.commons.lang.StringUtils.isNotBlank(selectNode) && selectNode.contains(product.getProductId().toString())){
						thirdNode.setSelected(true);
					}
				}
			}
		}

		return root;
	}
}
